package cn.virens.cmpt.trayicon;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;

import cn.hutool.core.io.FileUtil;
import cn.virens.App;
import cn.virens.app.AppConfig;
import cn.virens.app.cmpt.Component;

public class TrayIconComponent implements Component {
	private SystemTray mSystemTray = SystemTray.getSystemTray();

	private PopupMenu mPopupMenu = new PopupMenu();
	private MenuItem mMenuItem = new MenuItem("Stop");

	private String mImagePath = null;
	private TrayIcon mTrayIcon = null;
	private ImageIcon mImageIcon = null;

	@Override
	public void initialize(AppConfig config) throws Exception {
		if (!SystemTray.isSupported()) return;

		// 退出按钮
		mMenuItem.addActionListener((ActionEvent e) -> {
			App.app().exit();
		});

		// 托盘图标
		mImagePath = FileUtil.getAbsolutePath("classpath:favicon.png");
		mImageIcon = new ImageIcon(mImagePath);

		// 系统托盘
		mTrayIcon = new TrayIcon(mImageIcon.getImage());
		mTrayIcon.setPopupMenu(mPopupMenu);
		mTrayIcon.setImageAutoSize(true);
		mTrayIcon.setToolTip("test");

		// 系统托盘
		mPopupMenu.add(mMenuItem);
		mSystemTray.add(mTrayIcon);
	}

	@Override
	public void destroy() throws Exception {
		mSystemTray.remove(mTrayIcon);
	}
}
